package o2o.web.actions.shop;

import com.jinyou.utils.common.*;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import o2o.cache.shop.ShopExtraInfoMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.config.dao.UserDaoSql;
import o2o.config.dao.utils.DaoUtils;
import o2o.config.dao.utils.Expression;
import o2o.config.dao.utils.QueryCondition;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.common.UserType;
import o2o.dao.PO.shop.*;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.common.FileUploadInfo;
import o2o.dao.VO.shop.ShopVerifyDBVO;
import o2o.company.model.ShopInfo;
import o2o.system.data.SYS_CONFIG_PATH;
import o2o.web.actions.user.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

@Controller
public class ShopVerifyAction {

    //提交店铺审核
    @RequestMapping(method = RequestMethod.POST, value = "/shop/verify/add")
    @ResponseBody
    public String shop_add(String token, ShopVerifyDBVO info, FileUploadInfo fileUploadInfo, String shopType, Integer step) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }


        if (ValidateUtil.isNotAbsInteger(step)) {
            return AjaxResult.errorResult("输入有误，请稍后再试");
        }

//        if (ValidateUtil.isNull(info.getShopName())) {
//            return AjaxResult.errorResult("店铺名称不能为空");
//        }
//        if (ValidateUtil.isNull(info.getOwnUsername())) {
//            return AjaxResult.errorResult("店主账号不能为空");
//        }

        boolean isExactUserType = User.isExactUserType();

        Long shopId = null;
        if (null == info) {
            return AjaxResult.errorResult("请补全信息");
        }

        List<ShopInfo> shopInfo1 = ShopMemory.getInstance().getShopListByUserName(info.getOwnUsername());
        if (null != shopInfo1 && shopInfo1.size() > 0) {
            return AjaxResult.errorResult("当前店主账号已存在，不允许再添加新店铺");
        }

        if (ValidateUtil.isID(info.getId())) {
            QueryCondition query = QueryCondition.getInstance();
            Expression expression = Expression.getInstance();
            expression.addChild("delFlag", 0);//所有未删除的
            expression.addChild("id", info.getId());
            expression.addChild("verifyStatus", ShopVerify.ING);
            query.addCondition(expression);
            try {
                int count = UserDaoSql.getInstance().getCount(PojoTables.ShopVerify, query);
                if (count > 0) {
                    return AjaxResult.errorResult("您已提交申请，请耐心等待");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return AjaxResult.errorResult("获取失败，请稍后再试");
            }

            shopId = info.getId();
        } else {
            info.setId(null);
            info.setOwnUsername(user.getUsername());
        }

//        QueryCondition query = QueryCondition.getInstance();
//        Expression expression = Expression.getInstance();
//        expression.addChild("delFlag", 0);//所有未删除的
//        expression.addChild("isVerify", 0);//未审核
//        if (ValidateUtil.isNotNull(info.getCity())) {
//            expression.addChild("city", info.getCity());
//        }
//        if (ValidateUtil.isNotNull(info.getOwnUsername())) {
//            expression.addChild("ownUsername", info.getOwnUsername());
//        }
//        query.addCondition(expression);
//
//        try {
//            int count = UserDaoSql.getInstance().getCount(PojoTables.ShopVerify, query);
//            if (count > 0) {
//                return AjaxResult.errorResult("您已提交过申请，请耐心等待");
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return AjaxResult.errorResult("获取失败，请稍后再试");
//        }


//        if (null == fileUploadInfo || null == fileUploadInfo.getImage()) {
//            return AjaxResult.errorResult("图片不能为空");
//        } else {
//  //存储图片
        //        String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage());
        //        if (null == filePath) {
        //            return AjaxResult.errorResult("存储图片失败，请稍后再试");
        //        }
        //        info.setImageUrl(filePath);
//
//        }


        if (null != fileUploadInfo) {
            //店铺照片
            if (null != fileUploadInfo.getImage1()) {
                //存储图片
//                String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage1());
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage1());
                if (null == filePath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl1(filePath);
            }

            //店内环境图
            if (null != fileUploadInfo.getImage2()) {
                //存储图片
//                String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage2());
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage2());
                if (null == filePath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl2(filePath);
            }

            //门脸图
            if (null != fileUploadInfo.getImage3()) {
                //存储图片
//                String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage3());
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage3());
                if (null == filePath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl3(filePath);
            }

            //营业执照
            if (null != fileUploadInfo.getImage4()) {
                //存储图片
//                String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage4());
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage4());
                if (null == filePath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl4(filePath);
            }

            //身份证照
            if (null != fileUploadInfo.getImage5()) {
                //存储图片
//                String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage5());
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage5());
                if (null == filePath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl5(filePath);
            }
            // 身份证反面照片
            if (null != fileUploadInfo.getImage6()) {
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage6());
                if (null == filePath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl6(filePath);
            }

            // 手持身份证照
            if (null != fileUploadInfo.getImage7()) {
                //存储图片
//                String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage5());
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage7());
                if (null == filePath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl7(filePath);
            }

            // 合同照
            if (null != fileUploadInfo.getImage8()) {
                //存储图片
//                String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage5());
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage8());
                if (null == filePath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl8(filePath);
            }
            // 卫生许可照
            if (null != fileUploadInfo.getImage9()) {
                //存储图片
//                String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, fileUploadInfo.getImage5());
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage9());
                if (null == filePath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl9(filePath);
            }
            // 卫生许可照
            if (null != fileUploadInfo.getImage10()) {
                //存储图片
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage10());
                if (null == filePath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl10(filePath);
            }
            // 酒牌
            if (null != fileUploadInfo.getImage12()) {
                //存储图片
                String filePath = JYImageUtils.saveImage(SYS_CONFIG_PATH.SHOP_IMAGE, fileUploadInfo.getImage12());
                if (null == filePath) {
                    return AjaxResult.errorResult("存储图片失败，请稍后再试");
                }
                info.setImageUrl12(filePath);
            }
        }


//        if (shopImage == null) {
//            return AjaxResult.errorResult("图片不能为空");
//        }

//        if (null != shopImage) {
//            //存储图片
//            String filePath = JYImageUtils.saveImage(Common.SHOPIMAGE, shopImage);
//            if (null == filePath) {
//                return AjaxResult.errorResult("存储图片失败，请稍后再试");
//            }
//            info.setImageUrl(filePath);
//        }


        info.setCreateUser(user.getUsername());
        long time = DateUtils.getCurrTime();
        info.setCreateTime(time);
        info.setDelFlag(0);
        info.setIsOpen(1);
        info.setIsVerify(0);
        info.setShopTypes(shopType);

        //判断店主是否存在
        UserInfo userInfo = UserInfoMemory.getInstance().getUserInfo(info.getOwnUsername());
        if (null == userInfo) {//账号不存在
            //添加账号
//            try {
//                User.getInstance().addUser(info.getOwnUsername(), info.getOwnUsername().substring(5, 11), UserType.SHOP_OWNER);
//            } catch (Exception e) {
//                e.printStackTrace();
//                return AjaxResult.errorResult(e.getMessage());
//            }
        } else {//有这个账号，看这个用户的类型
            switch (userInfo.getUserType()) {
                case UserType.SHOP_OWNER: {//如果已经是店主了  那不允许再添加账号
                    List<ShopInfo> shopInfo = ShopMemory.getInstance().getShopListByUserName(userInfo.getUsername());
                    if (null != shopInfo && shopInfo.size() > 0) {
                        return AjaxResult.errorResult("当前店主账号已存在，不允许再添加新店铺");
                    }
                }
                break;
                case UserType.USER: { //普通用的话，升级为店主
//                    User.getInstance().modifyUserType(userInfo.getUsername(), UserType.SHOP_OWNER);
//                    userInfo.setUserType(UserType.SHOP_OWNER);
                }
                break;
                case UserType.POSTMAN: { //配送员的话也不允许
                    if (isExactUserType) {
                        // 骑手也能申请入驻商家

                    } else {
                        return AjaxResult.errorResult("当前店主账号是配送员，请更换店主账号");
                    }
//                    return AjaxResult.errorResult("当前店主账号是配送员，请更换店主账号");
                }
//                break;
                case UserType.ADMIN: {//管理员的话看看他有没有店铺
                    List<ShopInfo> shopInfo = ShopMemory.getInstance().getShopListByUserName(userInfo.getUsername());
                    if (null != shopInfo && shopInfo.size() > 0) {
                        return AjaxResult.errorResult("当前店主账号已存在，不允许再添加新店铺");
                    }
                }
                break;
            }
        }


        if (ValidateUtil.isAbsDouble(info.getLat()) && ValidateUtil.isAbsDouble(info.getLng())) {
            String geocode = Geohash.encode(info.getLat(), info.getLng());
            Long areaPoint = Geohash.getArea(info.getLat(), info.getLng());
            info.setGeocode(geocode);
            info.setAreaPoint(areaPoint);
        }


//        if (0 == 1 - step) {
//            //店铺信息保存到数据库
//            try {
//                UserDaoSql.getInstance().insertObj(PojoTables.ShopVerify, info);
//            } catch (SQLException e) {
//                e.printStackTrace();
//                return AjaxResult.errorResult("新增失败，请稍后再试");
//            }
//        }


        switch (step) {
            case 1: {//保存店铺名
                //店铺信息保存到数据库

                if (ValidateUtil.isID(shopId)) {//之前保存过
                    StringBuffer sb = new StringBuffer();
                    sb.append("update ").append(PojoTables.ShopVerify).append(" set shopType='").append(shopType).append("',shopName='")
                            .append(info.getShopName()).append("',updateUser = '").append(user.getUsername()).append("' where delFlag = 0 and id = ").append(shopId);

                    try {
                        UserDaoSql.getInstance().executeBySql(sb.toString());
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("新增失败，请稍后再试.");
                    }

                } else {//没有的新建
                    try {
                        info.setVerifyStatus(ShopVerify.SUBMIT);
                        UserDaoSql.getInstance().insertObj(PojoTables.ShopVerify, info);
                    } catch (SQLException e) {
                        e.printStackTrace();
                        return AjaxResult.errorResult("新增失败，请稍后再试");
                    }

                    //查询出刚才插入的数据
                    QueryCondition query = QueryCondition.getInstance();
                    query.addCondition(Expression.getInstance().addChild("delFlag", 0).addChild("createTime", time));//所有未删除的
                    String sql = DaoUtils.getSql(PojoTables.ShopVerify, query);
                    ShopVerify shopVerify = null;
                    try {
                        shopVerify = (ShopVerify) UserDaoSql.getInstance().getObject(ShopVerify.class, sql);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    if (null != shopVerify) {
                        shopId = shopVerify.getId();
                    }
                }

            }
            break;
            case 2: {//店铺地址电话等
                if (ValidateUtil.isNotID(info.getId())) {
                    return AjaxResult.errorResult("请选择要补充的信息");
                }

                StringBuffer sb = new StringBuffer();
                //是不是批发
                String isSeriesSale = "";
                if (ValidateUtil.isNotAbsInteger(info.getIsSeriesSale())) {
                    isSeriesSale = ",isSeriesSale=0";
                } else {
                    isSeriesSale = ",isSeriesSale=1";
                }

                // 商圈Id
                String agentIdStr = "";
                if (ValidateUtil.isID(info.getAgentId())) {
                    agentIdStr = ",agentId=" + info.getAgentId();
                }
                // 对区进行去空格操作
                String countyStr = "";
                if (ValidateUtil.isNotNull(info.getCounty())) {
                    countyStr = info.getCounty().replace(" ", "");
                }

                sb.append("update ").append(PojoTables.ShopVerify).append(" set lat = ").append(info.getLat())
                        .append(",lng = ").append(info.getLng()).append(",geocode='").append(info.getGeocode())
                        .append("',areaPoint=").append(info.getAreaPoint()).append(",province='").append(info.getProvince())
                        .append("',city='").append(info.getCity()).append("',county='").append(countyStr)
                        .append("',region='").append(info.getRegion()).append("',ownName='").append(info.getOwnName())
                        .append("',telephone='").append(info.getTelephone()).append("',imageUrl1='").append(info.getImageUrl1()).append("',address='").append(info.getAddress()).append("' ")
                        .append(agentIdStr)
                        .append(isSeriesSale)//是不是批发
                        .append(",verifyStatus=").append(ShopVerify.SUBMIT).append(",updateUser = '").append(user.getUsername()).append("' where id = ").append(info.getId());
                try {
                    UserDaoSql.getInstance().executeBySql(sb.toString());
                } catch (SQLException e) {
                    e.printStackTrace();
                    return AjaxResult.errorResult("上传信息失败，请稍后再试");
                }
            }
            break;
            case 3: {//店铺门脸照片
                if (ValidateUtil.isNotID(info.getId())) {
                    return AjaxResult.errorResult("请选择要补充的信息.");
                }

                StringBuffer sb = new StringBuffer();
                sb.append("update ").append(PojoTables.ShopVerify).append(" set imageUrl3='").append(info.getImageUrl3()).append("',imageUrl2='").append(info.getImageUrl2()).append("',updateUser='")
                        .append(user.getUsername()).append("',verifyStatus=").append(ShopVerify.SUBMIT).append(" where id = ").append(info.getId());
                try {
                    UserDaoSql.getInstance().executeBySql(sb.toString());
                } catch (SQLException e) {
                    e.printStackTrace();
                    return AjaxResult.errorResult("上传信息失败，请稍后再试");
                }
            }
            break;
            case 4: {//资质信息
                if (ValidateUtil.isNotID(info.getId())) {
                    return AjaxResult.errorResult("请选择要补充的信息.");
                }

                StringBuffer sb = new StringBuffer();
                sb.append("update ").append(PojoTables.ShopVerify).append(" set licence='").append(info.getLicence()).append("',licenceAddress='").append(info.getLicenceAddress())
                        .append("',licenceType=").append(info.getLicenceType()).append(",licenceDate=").append(info.getLicenceDate())
                        .append(",imageUrl4='").append(info.getImageUrl4()).append("',imageUrl5='").append(info.getImageUrl5()).append("',updateUser ='")
                        .append(user.getUsername()).append("',verifyStatus=").append(ShopVerify.ING).append(",owner='").append(info.getOwner()).append("',ownerID='")
                        .append(info.getOwnerID()).append("'");
                // 6
                if (StringUtils.isNotEmpty(info.getImageUrl6())) {
                    sb.append(",imageUrl6='").append(info.getImageUrl6()).append("'");
                }
                // 7
                if (StringUtils.isNotEmpty(info.getImageUrl7())) {
                    sb.append(",imageUrl7='").append(info.getImageUrl7()).append("'");
                }
                // 8
                if (StringUtils.isNotEmpty(info.getImageUrl8())) {
                    sb.append(",imageUrl8='").append(info.getImageUrl8()).append("'");
                }
                // 9
                if (StringUtils.isNotEmpty(info.getImageUrl9())) {
                    sb.append(",imageUrl9='").append(info.getImageUrl9()).append("'");
                }
                // 10
                if (StringUtils.isNotEmpty(info.getImageUrl10())) {
                    sb.append(",imageUrl10='").append(info.getImageUrl10()).append("'");
                }
                // 11
                if (StringUtils.isNotEmpty(info.getImageUrl11())) {
                    sb.append(",imageUrl11='").append(info.getImageUrl11()).append("'");
                }
                // 酒牌
                if (StringUtils.isNotEmpty(info.getImageUrl12())) {
                    sb.append(",imageUrl12='").append(info.getImageUrl12()).append("'");
                }
                if (StringUtils.isNotEmpty(info.getCompanyName())) {
                    sb.append(",companyName='").append(info.getCompanyName()).append("'");
                }
                if (StringUtils.isNotEmpty(info.getCompanyNumber())) {
                    sb.append(",companyNumber='").append(info.getCompanyNumber()).append("'");
                }
                if (StringUtils.isNotEmpty(info.getCompanyTaxNumber())) {
                    sb.append(",companyTaxNumber='").append(info.getCompanyTaxNumber()).append("'");
                }
                sb.append("  where id = ").append(info.getId());
                try {
                    UserDaoSql.getInstance().executeBySql(sb.toString());
                } catch (SQLException e) {
                    e.printStackTrace();
                    return AjaxResult.errorResult("上传信息失败，请稍后再试");
                }
            }
            break;
        }

        return AjaxResult.successResult(shopId + "");
    }



    private void verifyShopVerifyByExistShop(ShopInfo shopInfo, ShopVerify verify_over, ShopInfoExtra extra, ShopVerify toVerify, String opUser) throws SQLException {
        if (toVerify.getShopCategory() == null) {
            toVerify.setShopCategory(ShopCategoryMan.shopTypeGroup);
        }
        Connection conn = null;
        try {
            conn = UserDaoSql.getInstance().getConnection();
            conn.setAutoCommit(false);
            if (verify_over != null) {
                StringBuilder sql = new StringBuilder();
                sql.append(" update ").append(PojoTables.ShopVerify).append(" set shopCategory = (shopCategory|").append(toVerify.getShopCategory()).append(") where id = ").append(verify_over.getId())
                        .append(" and delFlag = 0");
                UserDaoSql.getInstance().executeBySql(sql.toString(), conn);
                StringBuilder delSql = new StringBuilder();
                delSql.append("update ").append(PojoTables.ShopVerify).append(" set isVerify =1,updateUser='")
                        .append(opUser).append("',verifyStatus=").append(ShopVerify.OVER)
                        .append(" ,delFlag = 1 ")
                        .append(" where delFlag = 0 and id =").append(toVerify.getId());
                UserDaoSql.getInstance().executeBySql(delSql.toString(), conn);
            } else {
                StringBuilder sb = new StringBuilder();
                sb.append("update ").append(PojoTables.ShopVerify).append(" set isVerify =1,updateUser='")
                        .append(opUser).append("',verifyStatus=").append(ShopVerify.OVER).append(" where delFlag = 0 and id =").append(toVerify.getId());
                UserDaoSql.getInstance().executeBySql(sb.toString());
            }
            // 更新其他信息
            if (extra != null && extra.getShopCategoryApplyState() - CLOUD_SHOP_APPLY_STATE.CLOUD_SHOP_APPLY_ING == 0) {
                StringBuilder extraSql = new StringBuilder();
                extraSql.append(" update ").append(PojoTables.SHOP_INFO_EXTRA).append(" set  ").append(" shopCategoryApplyState = ").append(CLOUD_SHOP_APPLY_STATE.CLOUD_SHOP_APPLY_PASS)
                        .append(" where delFlag = 0 and id = ").append(extra.getId());
                UserDaoSql.getInstance().executeBySql(extraSql.toString(), conn);
            }
            // 更新店铺
            StringBuilder shopSql = new StringBuilder();
            shopSql.append(" update ").append(PojoTables.ShopInfo)
                    .append(" set shopCategory = (shopCategory|").append(toVerify.getShopCategory()).append(") where delFlag =0  and id = ").append(shopInfo.getId());
            UserDaoSql.getInstance().executeBySql(shopSql.toString(), conn);
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            UserDaoSql.getInstance().rollback(conn);
            throw e;
        } finally {
            UserDaoSql.getInstance().close(conn);
        }
        // 更新缓存
        ShopMemory.getInstance().modifyById(shopInfo.getId());
        ShopExtraInfoMemory.getInstance().addOrModify(shopInfo.getId());
    }


    private static void rollBackForShopVerify(Long shopId) {
        if (ValidateUtil.isID(shopId)) {
            StringBuffer sql = new StringBuffer();
            sql.append("update ").append(PojoTables.ShopInfo)
                    .append(" set delFlag = 1 where id =").append(shopId).append(" and delFlag = 0");
            try {
                UserDaoSql.getInstance().executeBySql(sql.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 更新缓存
            ShopMemory.getInstance().delete(shopId);
            try {
                JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.SHOP_INFO_DELETE).setLong01(shopId);
                JmsSender.setTopicSender(notify);
            } catch (Exception e) {
            }
        }
    }

    /**
     * @param token
     * @param shopId
     * @param reason 原因
     * @return
     */
    //拒绝通过店铺
    @RequestMapping(method = RequestMethod.POST, value = "/shop/verify/refund")
    @ResponseBody
    public String shop_verify_refund(String token, String shopId, String reason) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }

        // 注意该Id为审核记录的id不是店铺Id
        if (ValidateUtil.isNull(shopId)) {
            return AjaxResult.errorResult("请选择要审核的店铺");
        }
        QueryCondition query = QueryCondition.getInstance();
        Expression expression = Expression.getInstance();
        expression.addChild("delFlag", 0);//所有未删除的
        expression.addChild("id", shopId);
        expression.addChild("verifyStatus", ShopVerify.ING);
        query.addCondition(expression);

        String sql = DaoUtils.getSql(PojoTables.ShopVerify, query);
        List<ShopVerify> list = null;
        try {
            list = (List<ShopVerify>) UserDaoSql.getInstance().list(ShopVerify.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("拒绝审核失败，请稍后再试");
        }
        if (!ValidateUtil.isAbsList(list)) {
            return AjaxResult.successResult();
        }
        StringBuffer buffer = new StringBuffer();
        buffer.append("update ").append(PojoTables.ShopVerify).append(" set ")
                .append(" verifyStatus =").append(ShopVerify.NO)
                .append(",isVerify = 0")
                .append(", updateUser ='").append(user.getUsername()).append("' ");

        if (StringUtils.isNotEmpty(reason)) {
            buffer.append(",refuseReason = '").append(reason).append("' ");
        }
        buffer.append(" where id =").append(shopId).append(" and delFlag = 0");
        try {
            UserDaoSql.getInstance().executeBySql(buffer.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult("拒绝失败,请稍后重试");
        }
        List<ShopInfo> shopInfoList = ShopMemory.getInstance().getShopListByUserName(list.get(0).getOwnUsername());
        ShopInfo shopInfo_db = null;
        if (ValidateUtil.isAbsList(shopInfoList)) {
            shopInfo_db = shopInfoList.get(0);
        }
        ShopInfoExtra infoExtra = null;
        if (shopInfo_db != null) {
            infoExtra = ShopExtraInfoMemory.getInstance().getShopExtraInfoById(shopInfo_db.getId());
        }
        if (infoExtra != null && infoExtra.getShopCategoryApplyState() - CLOUD_SHOP_APPLY_STATE.CLOUD_SHOP_APPLY_ING == 0) {
            StringBuilder extraSql = new StringBuilder();
            extraSql.append(" update ").append(PojoTables.SHOP_INFO_EXTRA)
                    .append(" set shopCategoryApplyState = ").append(CLOUD_SHOP_APPLY_STATE.CLOUD_SHOP_APPLY_NO_PASS)
                    .append(" where delFlag = 0 and id =").append(infoExtra.getId());
            try {
                UserDaoSql.getInstance().executeBySql(extraSql.toString());
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.errorResult("拒绝失败,请稍后重试");
            }
            ShopExtraInfoMemory.getInstance().addOrModify(infoExtra.getShopId());
        }
        return AjaxResult.successResult();
    }

}
